package htmlToWord;

import org.apache.poi.ss.usermodel.*;
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Element;

import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 * 描述:获取html标签属性
 *
 * @author yanzhengwu
 * @create 2020-12-30 11:14
 */
public class LabelAttribute {

    /**
     * 文档操作模型
     */
    private Workbook wb;
    /**
     * 预存需要处理的标签名称
     */
    public Set<String> labelName = new HashSet<>();

    /**
     * html单元格参数列表和样式列表
     */
    private Map<String, Object> attributeMap = new LinkedHashMap<>();

    public LabelAttribute(Workbook wb) {
        this.wb = wb;
    }

    public LabelAttribute() {
    }

    /**
     * Html一个单元格中参数列表和样式标签
     *
     * @param tdEle
     */
    public void getHtmlAttribute(Element tdEle) {
        labelName.add(tdEle.tagName());
        attributeMap.put(tdEle.tagName(), tdEle.tagName());
        Attributes attributes = tdEle.attributes();
        if (null != attributes && attributes.size() > 0) {
            for (Attribute attribute : attributes) {
                attributeMap.put(attribute.getKey(), attribute.getValue());
            }
        }
        if (null != tdEle.children() && tdEle.children().size() > 0) {
            for (Element e : tdEle.children()) {
                getHtmlAttribute(e);
            }
        }

    }

    public Map<String, Object> getAttributeMap() {
        return attributeMap;
    }

    public Map<String, Object> setAttributeMap(String key, Object value) {
        this.attributeMap.put(key, value);
        return this.attributeMap;
    }

    /**
     * 制作cell字体样式
     *
     * @return
     */
    public CellStyle makeCellStyle() {
        short fontSize = 14;
        String fontName = "宋体";
        CellStyle style = wb.createCellStyle();
        //添加上下左右边框
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setAlignment(HorizontalAlignment.CENTER);//设置水平居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);//设置垂直居中

        Font font = wb.createFont();
        //font.setColor((short) 2);//字体颜色
        font.setFontName(fontName);//字体名称
        font.setFontHeightInPoints(fontSize);//字体大小

        for (Map.Entry<String, Object> entry : attributeMap.entrySet()) {
            String key = entry.getKey();
            switch (key) {
                case "i":
                    font.setItalic(true);//斜体
                    System.out.println("获取到字体[倾斜]");
                    break;
                case "strong":
                    font.setBold(true);//加粗
                    System.out.println("获取到字体[加粗]");
                    break;
                case "u":
                    font.setUnderline((byte) 1);//下划线
                    System.out.println("获取到字体[下划线]");
                    break;
                case "s":
                    font.setStrikeout(true);//删除线
                    System.out.println("获取到字体[删除线]");
                    break;

                default:

            }
        }
        style.setFont(font);

        return style;
    }

}
